<?php
/*
 module:		红娘管理
 create_time:	2022-03-04 16:27:06
 author:		
 contact:		
*/

namespace app\backend\controller;

use app\backend\service\ZhMatchmakerLevelService;
use app\backend\service\ZhMatchmakerService;
use app\backend\model\ZhMatchmakerLevel as ZhMatchmakerLevelModel;
use app\backend\model\Member as MemberModel;

class ZhMatchmakerLevel extends Admin
{
    /*首页数据列表*/
    function index()
    {
        if (!$this->request->isAjax()) {
            return view('index');
        } else {
            $limit           = $this->request->post('limit', 20, 'intval');
            $offset          = $this->request->post('offset', 0, 'intval');
            $where['name']   = $this->request->param('name_s', '', 'serach_in');
            $where['status'] = $this->request->param('status', '', 'serach_in');

            $page = floor($offset / $limit) + 1;
            $res  = ZhMatchmakerLevelService::indexList(formatWhere($where), $limit, $page);
            return json($res);
        }
    }

    /*修改*/
    function update()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get('id', '', 'serach_in');
            if (!$id) $this->error('参数错误');
            $this->view->assign('info', checkData(ZhMatchmakerLevelModel::find($id)));
            return view('update');
        } else {
            $postField = 'zs_price,is_zs,name,unit,createtime,expiry,status,id,up_num';
            $data      = $this->request->only(explode(',', $postField), 'post', null);

            $res = ZhMatchmakerLevelService::update($data);
            return json(['status' => '00', 'msg' => '修改成功']);
        }
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @author  lwx
     */
    function add()
    {
        if (!$this->request->isPost()) {
            return view('add');
        } else {
            $postField          = 'zs_price,is_zs,name,unit,expiry,status,up_num';
            $data               = $this->request->only(explode(',', $postField), 'post', null);
            $data['createtime'] = time();
            $res                = ZhMatchmakerLevelModel::create($data);
            return json(['status' => '00', 'msg' => '添加成功']);
        }
    }

    /*删除*/
    function delete()
    {
        $idx = $this->request->post('id', '', 'serach_in');
        if (!$idx) $this->error('参数错误');
        try {
            ZhMatchmakerLevelModel::destroy(['id' => explode(',', $idx)], true);
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return json(['status' => '00', 'msg' => '操作成功']);
    }

    /*查看详情*/
    function view()
    {
        $matchmaker_id = $this->request->get('matchmaker_id', '', 'serach_in');
        if (!$matchmaker_id) $this->error('参数错误');
        $this->view->assign('info', ZhMatchmakerModel::find($matchmaker_id));
        return view('view');
    }

}

